﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections;

namespace GameLogicProject
{
    class Program
    {
        static void Main(string[] args)
        {
            string fich = Console.ReadLine();
            fich = "IPhoneXML.xml";
            Player player = new Player("Jacinto");
            List<Option> marketsList = loadGame(fich);

            /*foreach (Option market in marketsList)
            {
                Console.WriteLine("Mercado: " + market.Name);
                foreach (Option child in market.Options)
                {
                    Console.WriteLine(child.Name);
                }
            }
            Console.ReadKey();*/


            Option markets = new Option();

            markets.Options = marketsList;
            player.ChoiceTree = markets;

            player.chooseMarket();


            do
            {
                Console.WriteLine("\n-------- Nova Semana! --------");
                player.chooseTree();
                Console.WriteLine("\n\nPercentagem: " + player.getCurrentPercentagem());
                Console.WriteLine("Income: " + player.generateWeekIncome());
            } while (Console.ReadKey().Key != ConsoleKey.Escape);


            Console.ReadKey();
        }





        /*public static bool loadGame2(string nomeFicheiro)
        {
            XmlDocument loadFich = new XmlDocument();
            loadFich.LoadXml(no)
        }*/

        public static List<Option> loadGame(string nomeFicheiro)
        {
            /*XmlReader ficheiro = XmlReader.Create(nomeFicheiro);
            Console.WriteLine(nomeFicheiro);*/

            XmlDocument loadFich = new XmlDocument();

            loadFich.Load(nomeFicheiro);
            XmlNodeList listMarkets = loadFich.GetElementsByTagName("Market");

            List<Option> marketsOptions = new List<Option>();

            //Percorre Markets
            foreach (XmlNode market in listMarkets)
            {
                
                //Nome do mercado
                string marketName = market["Name"].InnerXml;
                //Console.WriteLine("" + marketName);
                //Influencia
                string influence = market["Influence"].InnerXml;
                //Console.WriteLine("" + influence);
                //Descricao
                string description = market["Description"].InnerXml;
                //Console.WriteLine("" + description);

                string finalNode = market.Attributes["finalNode"].InnerXml;
                //Console.WriteLine("" + finalNode);

                Option MarketOpt = new Option(marketName, description, double.Parse(influence));
                MarketOpt.finalNode = bool.Parse(finalNode);

                ArrayList talksArray = new ArrayList();
                ArrayList subOptions = new ArrayList();

                //Percorre filhos do mercado
                foreach (XmlNode marketChild in market.ChildNodes)
                {
                    //Caso seja talksOption
                    if (marketChild.Name.CompareTo("TalksOption") == 0)
                    {
                        XmlNodeList talksList = marketChild.ChildNodes;

                        //Percorre as talks
                        foreach (XmlNode talk in talksList)
                        {
                            string talkString = talk.InnerXml;
                            talksArray.Add(talkString);
                            //Console.WriteLine(talkString);
                        }
                    }
                    //Caso seja SubOptions
                    else if (marketChild.Name.CompareTo("SubOptions") == 0)
                    {
                        XmlNodeList optionsList = marketChild.ChildNodes;

                        //Percorre option
                        foreach(XmlNode option in optionsList) 
                        {
                            string optionString = option.InnerXml;
                            //Console.WriteLine(optionString);
                            subOptions.Add(optionString);
                        }
                    }
                }

                MarketOpt.Talks = talksArray;
                MarketOpt.OptionsName = subOptions;
                marketsOptions.Add(MarketOpt);
            }

            //Console.WriteLine("\n\nOPTIONS:");
            XmlNodeList listOptions = loadFich.GetElementsByTagName("Option");

            List<Option> allOptions = new List<Option>();

            //Percorre options
            foreach (XmlNode option in listOptions)
            {
                string optionName = option["Name"].InnerText;
                //Console.WriteLine(optionName);
                string attName = option.Attributes["name"].InnerXml;
                //Console.WriteLine("     " + attName);
                string marketName = option.Attributes["market"].InnerXml;
                //Console.WriteLine("     " + marketName);
                string finalNode = option.Attributes["finalNode"].InnerXml;
                //Console.WriteLine("     " + finalNode);
                string optionInfluence = option["Influence"].InnerXml;
                //Console.WriteLine("     " + optionInfluence);
                string optionDescription = option["Description"].InnerXml;
                //Console.WriteLine("     " + optionDescription);

                Option opt = new Option(optionName, optionDescription, double.Parse(optionInfluence));
                opt.attName = attName;
                opt.market = marketName;
                opt.finalNode = bool.Parse(finalNode);

                ArrayList subOptions = new ArrayList();
                ArrayList talksArray = new ArrayList();

                XmlNodeList optionList = option.ChildNodes;
                foreach (XmlNode optionChild in optionList)
                {
                    if (optionChild.Name.CompareTo("SubOptions") == 0)
                    {
                        foreach (XmlNode subOption in optionChild.ChildNodes)
                        {
                            string subOptionName = subOption.InnerXml;
                            subOptions.Add(subOptionName);
                            //Console.WriteLine("     " + subOptionName);
                        }
                    }
                    else if (optionChild.Name.CompareTo("TalksOption") == 0)
                    {
                        foreach (XmlNode talk in optionChild.ChildNodes)
                        {
                            string talkString = talk.InnerXml;
                            talksArray.Add(talkString);
                            //Console.WriteLine("     " + talkString);
                        }
                    }
                }

                opt.OptionsName = subOptions;
                opt.Talks = talksArray;
                allOptions.Add(opt);
            }
            //Console.WriteLine("\n\nOPTIONS: " + allOptions.Count);

            //Percorre todas as options e cria a arvore
            foreach (Option optFather in allOptions)
            {
                //Percorre nomes das subOptions
                foreach (string optChildName in optFather.OptionsName)
                {
                    //Percorre todas as options
                    for (int i = 0; i < allOptions.Count; i++ )
                    {
                        if (((Option)allOptions[i]).attName.CompareTo(optChildName) == 0 && ((Option)allOptions[i]).market.CompareTo(optFather.market) == 0)
                        {
                            optFather.Options.Add((Option)allOptions[i]);
                        }
                    }
                }
            }

            //Percorre mercados
            foreach (Option mrk in marketsOptions)
            {
                //Percorre nomes das subOptions
                foreach (string optName in mrk.OptionsName)
                {
                    //Percorre percorre as options
                    for (int i = 0; i < allOptions.Count; i++ )
                    {
                        if (((Option)allOptions[i]).attName.CompareTo(optName) == 0 && ((Option)allOptions[i]).market.CompareTo(mrk.Name) == 0)
                        {
                            mrk.Options.Add((Option)allOptions[i]);
                        }
                    }
                }
            }

            return marketsOptions;
        }
    }
}
